package com.oncliedisk.administrator.ui;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.oncliedisk.administrator.content.OnlineConfig;
import com.oncliedisk.administrator.onlinedisk.R;
import com.vdisk.android.VDiskAuthSession;
import com.vdisk.android.VDiskDialogListener;
import com.vdisk.net.exception.VDiskDialogError;
import com.vdisk.net.exception.VDiskException;
import com.vdisk.net.session.AccessToken;
import com.vdisk.net.session.AppKeyPair;
import com.vdisk.net.session.Session;

/**
 * Created by Administrator on 2015/12/11.
 */
public class LoginActivity extends Activity implements VDiskDialogListener {
    private VDiskAuthSession session;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initData();
    }

    private void initData() {
        AppKeyPair appKeyPair = new AppKeyPair(OnlineConfig.CONSUMER_KEY, OnlineConfig.CONSUMER_SECRET);
        session = VDiskAuthSession.getInstance(this, appKeyPair,
                Session.AccessType.APP_FOLDER);
        if (session.isLinked()) {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }
    }

    /**
     * 点击登录
     *
     * @param view
     */
    public void onClick(View view) {
        session.setRedirectUrl(OnlineConfig.REDIRECT_URL);
        session.authorize(LoginActivity.this, LoginActivity.this);
    }


    public Activity getActivity() {
        return this;
    }


    /**
     * 认证结束后的回调方法
     * <p/>
     * Callback method after authentication.
     */
    @Override
    public void onComplete(Bundle values) {

        if (values != null) {
            AccessToken mToken = (AccessToken) values
                    .getSerializable(VDiskAuthSession.OAUTH2_TOKEN);
            session.finishAuthorize(mToken);
            Log.i("info","token"+mToken.getToken());
        }
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    /**
     * 认证出错的回调方法
     * <p/>
     * Callback method for authentication errors.
     */
    @Override
    public void onError(VDiskDialogError error) {
        Toast.makeText(getApplicationContext(),
                "Auth error : " + error.getMessage(), Toast.LENGTH_LONG).show();
    }

    /**
     * 认证异常的回调方法
     * <p/>
     * Callback method for authentication exceptions.
     */
    @Override
    public void onVDiskException(VDiskException exception) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(),
                "Auth exception : " + exception.getMessage(), Toast.LENGTH_LONG)
                .show();
    }

    /**
     * 认证被取消的回调方法
     * <p/>
     * Callback method as authentication is canceled.
     */
    @Override
    public void onCancel() {
        Toast.makeText(getApplicationContext(), "Auth cancel",
                Toast.LENGTH_LONG).show();
    }
}
